//
//  ContractReviewViewController.m
//  Jyxt
//
//  Created by 宫璞 on 2017/9/1.
//  Copyright © 2017年 DaLian Rental Housing Management. All rights reserved.
//

#import "ContractReviewViewController.h"
#import "EVhomeSection.h"
#import "ETVCcontractDetailHouseOwner.h"
#import "ScontractDetailViewModel.h"
#import "UserCenterViewModel.h"
#import "ETVCcontractDetailTeantTableViewCell.h"
#import "ETVCrentObjectTableViewCell.h"
#import "ETVCrentLimitTableViewCell.h"
#import "ETVCrentMoneyTableViewCell.h"
#import "ETVCdepositTableViewCell.h"
#import "RealUserViewController.h"

#define GETWIDTH        [UIScreen mainScreen].bounds.size.width
#define GETHEIGHT       [UIScreen mainScreen].bounds.size.height
@interface ContractReviewViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong) UITableView *contractDetailTableView;
@property ETVCcontractDetailHouseOwner *houseOwner;
@property (nonatomic, strong) NSMutableArray *contractDetailArr;


@end

@implementation ContractReviewViewController


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
   [self createNet];
}

- (void)initView{
    self.title = @"合同详情";
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.contractDetailTableView = [[UITableView alloc] initWithFrame: CGRectMake(0, 44.5, GETWIDTH, GETHEIGHT - 44.5) style: UITableViewStylePlain];
    self.contractDetailTableView.delegate = self;
    self.contractDetailTableView.dataSource = self;
    self.contractDetailTableView.clipsToBounds = YES;
    self.contractDetailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview: self.contractDetailTableView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}

- (void)createNet{
     self.contractDetailArr = [NSMutableArray new];
//    self.contractID = [[NSUserDefaults standardUserDefaults] objectForKey:@"CONTRACTDETAILCID"];
    [[ScontractDetailViewModel sharedInstance] requestContractDetailWithUtoken:[UserCenterViewModel getLocalUToken] id:self.contractID  success:^(NSArray *array) {
        [self.contractDetailArr addObjectsFromArray:array];
        [self.contractDetailTableView reloadData];
        
    } Fail:^(NSError *error) {
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 7;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        if (indexPath.section == 0) {
            return 44 * 7;
        }else if (indexPath.section == 1){
            return 44 * 7;
        }else if (indexPath.section == 2){
            return 44 * 5;
        }else if (indexPath.section == 3){
            return  44 * 3;
        }else if (indexPath.section == 4){
            return 44 * 2;
        }else{
            return 44;
        }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0 ||section == 1 || section == 2 || section == 3 || section == 4 || section == 5) {
        return 10;
    }
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *indentity00 = @"indentity00";
    NSString *indentity = @"indentity";
    NSString *indentity0 = @"indentity0";
    NSString *indentity1 = @"indentity1";
    NSString *indentity2 = @"indentity2";
    NSString *indentity3 = @"indentity3";
    NSString *indentity4 = @"indentity4";
    NSString *indentity5 = @"indentity5";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentity];
    if (cell == nil) {
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentity];
        if (indexPath.section == 0) {
            cell = [[ETVCcontractDetailHouseOwner alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentity0];
        }else if (indexPath.section == 1){
            cell = [[ETVCcontractDetailTeantTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentity1];
        }else if (indexPath.section == 2){
            cell = [[ETVCrentObjectTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentity2];
        }else if (indexPath.section == 3){
            cell = [[ETVCrentLimitTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentity3];
        }else if (indexPath.section == 4){
            cell = [[ETVCrentMoneyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentity4];
        }else if (indexPath.section == 5){
            cell = [[ETVCdepositTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentity5];
        }
        else{
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentity00];
            cell.textLabel.text = @"实际使用人";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    }
    
    if (self.contractDetailArr.count > 0) {
        if (indexPath.section == 0) {
            McontractDetailModel *model = [self.contractDetailArr objectAtIndex:0];
            [((ETVCcontractDetailHouseOwner *) cell) setContent:model];
        }else if (indexPath.section == 1){
            McontractDetailModel *model = [self.contractDetailArr objectAtIndex:0];
            [((ETVCcontractDetailTeantTableViewCell *) cell) setContent:model];
        }else if (indexPath.section == 2){
            McontractDetailModel *model = [self.contractDetailArr objectAtIndex:0];
            [((ETVCrentObjectTableViewCell *) cell) setContent:model];
        }else if (indexPath.section == 3){
            McontractDetailModel *model = [self.contractDetailArr objectAtIndex:0];
            [((ETVCrentLimitTableViewCell *) cell) setContent:model];
        }else if (indexPath.section == 4){
            McontractDetailModel *model = [self.contractDetailArr objectAtIndex:0];
            [((ETVCrentMoneyTableViewCell *) cell) setContent:model];
        } else if (indexPath.section == 5){
            McontractDetailModel *model = [self.contractDetailArr objectAtIndex:0];
            [((ETVCdepositTableViewCell *) cell) setContent:model];
        }
    }
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0 || section == 1 || section == 2 || section == 3 || section == 4) {
        return 30;
    }else{
        return 20;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    EVhomeSection *view = [[EVhomeSection alloc] initWithFrame: CGRectMake(0, 0, GETWIDTH, 24)];
    if (section == 0) {
        view.titleLabel.text = @"出租方";
    } else if (section == 1){
        view.titleLabel.text = @"承租方";
    }else if (section == 2){
        view.titleLabel.text = @"租赁标的";
    }else if (section == 3){
        view.titleLabel.text = @"租赁期限";
    }else if (section == 4){
        view.titleLabel.text = @"租金";
    }
    else{
        view.titleLabel.text = @"";
    }
    return view;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 6) {
        RealUserViewController *real = [[RealUserViewController alloc]init];
        real.contractId = self.contractID;
        [LKFreeCtrl pushViewController:real];
    }
}


@end
